home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
8bit
/
cislib_b
/
pasexp.doc
< prev
next >
Wrap
Text File
|
1995-04-22
|
2KB
|
52 lines
EXPONENTIATION IN PASCAL
A common question asked to Kyan is "How do I raise a number to a
power?" Standard Pascal provides no means of doing this, which is
usually implemented with a statement such as:
POWER=BASE^NUM:REM BASIC
or...
Power := Base ** Num; (* Some implementations of Pascal *)
Fortunately, there is a short and simple routine that can do it in
Standard Pascal. Its main statement is:
Power := Exp(Num * Ln(Base));
(You will recall that Exp and Ln are predefined, Standard Pascal
functions.) You can call this function with two real numbers; the
first number is the base, and the second is the number you wish to
raise the base to. Here's an example of calling Power:
NewNum := Power(Base,Raiser);
That is the 'quick & dirty' method of raising a number to a power.
'Quick' applies only to the fact that it is one line long; this method
of exponentiation is not very efficient and may take too long to do
the calculation. 'Dirty' applies to the limitation of not accepting
negative numbers and the fact that round-off errors may result. For
casual programming problems, this routine is acceptable, but for
more advanced problems which require speed, efficiency, and
accuracy, the longer version of Power must be used.
This longer Power routine is based on one written by Jon Snader
(who has a Ph.D in mathematics) which appeared in the September
1986 issue of Computer Language magazine. Before you use this
longer version of Power, make sure that you have included the file
FRAC.I, which is available in the data library under the filename
FRAC.PAS--see FRAC.DOC, too. This Power is called just like the
'lite' version of Power:
NewNum := Power(Base,Raiser);
NOTE: Since these function both have the same name, one can not
include them both in a program (although I don't know why one would
want to). When you download them, they are BOTH in the file
PASEXP.PAS--be sure to separate them with the Kyan Text Editor
and make sure that they are saved with different filenames on the
disk. For the 'lite' version, I recommend PWR.I as a filename, and for
the 'pro' version, POWER.I.